package com.cchip.cloud;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import com.cchip.cloud.api.MeetingApi;
import com.cchip.cloud.api.MeetingReturned;
import com.cchip.cloud.system.Account;
import com.cchip.cloud.system.BackgroundRunner;
import com.cchip.cloud.system.Config;

public class SettingActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {

	private static final String KEY_ROOM_ID = "key.room.id";
	private static final String KEY_ROOM_PASSWORD = "key.room.password";
	private static final String KEY_SET_USER_PASSWORD = "set.user.password";
	private static final String KEY_SET_ROOM_PASSWORD = "set.room.password";

	private Preference mUserRoomIdPref = null;
	private Preference mUserRoomPasswordPref = null;

	private MeetingApi mMeetingApi = MeetingApi.getInstance();
	private Requester mRequester = null;
	private Refresher mRefresher = null;

	private static final int REQ_SET_ROOM_PASSWORD = 16;

	private static final int REFRESH_HINT_SUCCESS = 28;

	private PreferenceActivity context = null;
	private ProgressDialog mWaitDialog = null;

	private SharedPreferences sp = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		getPreferenceManager().setSharedPreferencesName(Config.FILE_NAME);
		addPreferencesFromResource(R.xml.setting);

		sp = getPreferenceManager().getSharedPreferences();
		sp.registerOnSharedPreferenceChangeListener(this);

		mUserRoomIdPref = findPreference(KEY_ROOM_ID);
		mUserRoomPasswordPref = findPreference(KEY_ROOM_PASSWORD);

		mUserRoomIdPref.setSummary(Account.my.roomId);
		if (TextUtils.isEmpty(Account.my.roomPassword)) {
			mUserRoomPasswordPref.setSummary("无密码");
		} else {
			mUserRoomPasswordPref.setSummary(Account.my.roomPassword);
		}

		mRequester = new Requester(BackgroundRunner.getRunner());
		mRefresher = new Refresher();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		sp.unregisterOnSharedPreferenceChangeListener(this);

		Editor editor = sp.edit();
		editor.putString(KEY_SET_USER_PASSWORD, null);
		editor.putString(KEY_SET_ROOM_PASSWORD, null);
		editor.commit();
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals(KEY_SET_ROOM_PASSWORD)) {
			showWaitDialog();
			mRequester.sendEmptyMessage(REQ_SET_ROOM_PASSWORD);
		}
	}

	private void showWaitDialog() {
		mWaitDialog = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
		mWaitDialog.setMessage("资料提交中...");
		mWaitDialog.setCancelable(false);
		mWaitDialog.show();
	}

	private class Requester extends Handler {

		public Requester(Looper looper) {
			super(looper);
		}

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			MeetingReturned ret = null;
			switch (msg.what) {
			case REQ_SET_ROOM_PASSWORD:
				String roompassword = sp.getString(KEY_SET_ROOM_PASSWORD, "");
				ret = mMeetingApi.setRoomPassword(Account.my.name,
						Account.my.password, roompassword);
				if (ret != null) {
					if (ret.operCode == MeetingReturned.CODE_OK) {
						mRefresher.sendEmptyMessage(REFRESH_HINT_SUCCESS);
						Account.my.roomPassword = roompassword;
						Config.setRoomPassword(roompassword);
					} else {
						mRefresher.sendEmptyMessage(ret.operCode);
					}
				} else {
					mRefresher
							.sendEmptyMessage(MeetingReturned.CODE_NO_RESPONSE);
				}
				break;
			}
		}

	}

	private class Refresher extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			mWaitDialog.cancel();
			String hint = null;
			switch (msg.what) {
			case REFRESH_HINT_SUCCESS:
				hint = "密码修改成功";
				break;
			case MeetingReturned.CODE_PASSWORD_WRONG:
				hint = "密码有误，请检查您的输入";
				break;
			case MeetingReturned.CODE_ROOM_PASSOWRD_INVALID:
				hint = "会议室密码不符合要求，请更换再试";
				break;
			case MeetingReturned.CODE_NO_RESPONSE:
				hint = "服务器无响应，请稍后再试";
				break;
			default:
				hint = "密码修改失败，请重新输入";
				break;
			}

			if (hint != null) {
				Toast toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}

			mUserRoomPasswordPref.setSummary(Account.my.roomPassword);
		}
	}

}
